﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SubSonic;
using CXDB;
using Coolite.Ext.Web;

public partial class Company_UserListReply : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            Store1Databind();
            Store2Databind();
            Store3Databind();
        }
        CXSecurity.ChkBoss(Cookies.getCookies("cuid"));
    }
    protected void Store1_OnRefreshData(object s, StoreRefreshDataEventArgs e)
    {
        Store1Databind();
    }
    protected void Store1Databind()
    {
        Query q=new Query(VwCompanyPingjium.Schema);
        if (Request["type"] != null)
        {
            q.AddWhere("other_master_id", Cookies.getCookies("cuid"));
            q.AddWhere("dept_master_status", 1);
            q.AddWhere("manager_status", 0);
            Panel8.Visible = false;
            BtnDeptSub.Visible = false;
            
            tfdept_master_remark.Disabled = true;
            tfdeptmaster_status.Disabled = true;
            tfother_master_id.Disabled = true;
            tfmanager_id.Disabled = true;
            
            Panel7.Expand();
            Panel10.Expand();
        }
        else
        {
            if (Cookies.getCookies("cdeptid") != "1")
            {
                q.AddWhere("dept_master_id", Cookies.getCookies("cuid"));
                q.AddWhere("dept_master_status", SubSonic.Comparison.NotEquals, 2);
                Panel7.Expand();
                Panel10.Expand();
                Panel8.Expand(); 
                Button1.Visible = false;
                btnmanagersub.Visible = false;
                tfmanager_remark.Disabled = true;
                tfManager_status.Disabled = true;
                tfother_master_remark.Disabled = true;
                tfother_master_status.Disabled = true;
                
            }
            else
            {
                q.AddWhere("manager_id", Cookies.getCookies("cuid"));
                q.AddWhere("dept_master_status", 1);
                q.AddWhere("other_master_id", SubSonic.Comparison.Is, null).OR("other_master_id", SubSonic.Comparison.GreaterThan, 0).AND("other_master_status", 1);
                q.AddWhere("manager_status", SubSonic.Comparison.NotEquals, 2);
                BtnDeptSub.Visible = false;
                Button1.Visible = false;
                tfother_master_id.Disabled = true;
                tfother_master_remark.Disabled = true;
                tfother_master_status.Disabled = true;
                tfdeptmaster_status.Disabled = true;
                tfdept_master_remark.Disabled = true;
                tfmanager_id.Disabled = true;
            }
        }
        q.SetSelectList("id,companyid,companyname,dept_master_status,manager_status,intime,other_master_status,enter_deptname,enter_username");
        q.ORDER_BY("id desc"); 
        Store1.DataSource = q.ExecuteDataSet();
        Store1.DataBind();
    }
    protected void RowSelect(object s, AjaxEventArgs e)
    {
        string id = e.ExtraParams["id"].ToString();
        tfid.Value = id;
        Query q = new Query(CxCompanyPingJium.Schema);
        q.AddWhere("id", id);
        IDataReader dr = q.ExecuteReader();
        if(dr.Read())
        {
            guid.Value = dr["guid"].ToString();

            string[] str = CXFileUpload.show(new Guid(dr["guid"].ToString())).Split('$');
            tffileshow.NavigateUrl = str[0];
            tffileshow.Text = str[1];

            tfcompanyid.Text = dr["companyid"].ToString();
            tfcompanyname.Text = dr["companyname"].ToString();
            tfunfee_time.Text = dr["unfee_time"].ToString();
            tfunfee_cash.Text = dr["unfee_cash"].ToString();
            tfunfee_detail.Text = dr["unfee_detail"].ToString();
            tfthief_time1.Text = dr["thief_time1"].ToString();
            tfthief_time2.Text = dr["thief_time2"].ToString();
            tfthief_cash.Text = dr["thief_cash"].ToString();
            tfthief_detail.Text = dr["thief_detail"].ToString();
            tfuncoop_peak_time.Text = dr["uncoop_peak_time"].ToString();
            tfuncoop_peak_detail.Text = dr["uncoop_peak_detail"].ToString();
            tfuncoop_limit_time.Text = dr["uncoop_limit_time"].ToString();
            tfuncoop_limit_detail.Text = dr["uncoop_limit_detail"].ToString();
            //tfdept_master_time.Text = dr["dept_master_entering_time"].ToString();
            tfdept_master_remark.Text = dr["dept_master_remark"].ToString();
            tfuncoop_others.Text = dr["uncoop_others"].ToString();
            tfdept_master_remark.Text = dr["dept_master_remark"].ToString();
            //tfdept_master_time.Text = dr["dept_master_entering_time"].ToString();
            tfdeptmaster_status.SetInitValue(dr["dept_master_status"].ToString());
            tfmanager_remark.Text = dr["manager_remark"].ToString();
            //tfmanager_remark_time.Text = dr["manager_remark_time"].ToString();
            tfManager_status.SetInitValue(dr["manager_status"].ToString());
            tfmanager_id.SetInitValue(dr["manager_id"].ToString());
            tfother_master_id.SetInitValue(dr["other_master_id"].ToString());
            tfother_master_remark.Text = dr["other_master_remark"].ToString();
            tfother_master_status.SetInitValue(dr["other_master_status"].ToString());
            if(Cookies.getCookies("cuid")!=dr["dept_master_id"].ToString())
            {
                Panel8.Expand();
                BtnDeptSub.Disabled = true;
            }
            if(Cookies.getCookies("cuid")!=dr["manager_id"].ToString())
            {
                Panel7.Expand();
                btnmanagersub.Disabled = true;
            }
            if (dr["manager_status"].ToString() == "1" && dr["dept_master_status"].ToString() == dr["manager_status"].ToString())
            {
                BtnDeptSub.Disabled = true;
                btnmanagersub.Disabled = true;
            }
        }
        
    }
    protected void SumForm(object s,AjaxEventArgs e)
    {
        if (tfid.Value.ToString() == "")
        {
            Err.Warning("请从左侧列表中选择记录！", MessageBox.Icon.WARNING);
            return;
        }
        if (tfdept_master_remark.Text.Trim() == "")
        {
            Err.Warning("请填写意见内容！", MessageBox.Icon.WARNING);
            return;
        }
        if (tfdeptmaster_status.SelectedItem.Value == "")
        {
            Err.Warning("请选择审核状态！", MessageBox.Icon.WARNING);
            return;
        }
        if (tfmanager_id.SelectedItem.Value == "")
        {
            Err.Warning("请选择公司领导！", MessageBox.Icon.WARNING);
            return;
        }
        CxCompanyPingJium cx = new CxCompanyPingJium("id", tfid.Value.ToString());
        cx.DeptMasterEnteringTime = DateTime.Now;
        if (tfdeptmaster_status.SelectedItem.Value != "2")
        {
            cx.ManagerId = int.Parse(tfmanager_id.SelectedItem.Value.ToString());
            cx.ManagerStatus = 0;
            if (tfother_master_id.SelectedIndex != -1)
            {
                cx.OtherMasterId = int.Parse(tfother_master_id.SelectedItem.Value.ToString());
                cx.OtherMasterStatus = 0;
            }
           
        } 
        
        cx.DeptMasterRemark = tfdept_master_remark.Text;
        cx.DeptMasterStatus = int.Parse(tfdeptmaster_status.SelectedItem.Value);
        cx.Save();
        //日志记录
        LogRecord.Add(new Guid(guid.Value.ToString()), int.Parse(Cookies.getCookies("cuid")), tfdept_master_remark.Text,
                      int.Parse(tfdeptmaster_status.SelectedItem.Value));
         
        Clearform();
        Panel7.Collapse();
        Err.Warning("批复成功！", MessageBox.Icon.NONE);
        
    }
    protected void subother(object s, AjaxEventArgs e)
    {
        if (tfid.Value.ToString() == "")
        {
            Err.Warning("请从左侧列表中选择记录！", MessageBox.Icon.WARNING);
            return;
        }
        if (tfother_master_status.SelectedIndex == -1)
        {
            Err.Warning("请选择审核状态！",MessageBox.Icon.WARNING);
            return;
        }
        if (tfother_master_remark.Text.Trim() == "")
        {
            Err.Warning("审核意见不能为空！",MessageBox.Icon.WARNING);
            return;
        }
        CxCompanyPingJium cx = new CxCompanyPingJium("id", tfid.Value.ToString());
        cx.OtherMasterRemark = tfother_master_remark.Text;
        cx.OtherMasterStatus = int.Parse(tfother_master_status.SelectedItem.Value.ToString());
        cx.OtherMasterTime = DateTime.Now;
        cx.Save();
        LogRecord.Add(new Guid(guid.Value.ToString()), int.Parse(Cookies.getCookies("cuid")), tfother_master_remark.Text, int.Parse(tfother_master_status.SelectedItem.Value.ToString()));
        Clearform();
        Err.Warning("批复成功！",MessageBox.Icon.NONE);
        
    }
    protected void submanager(object s,AjaxEventArgs e)
    {
        if (tfid.Value.ToString() == "")
        {
            Err.Warning("请从左侧列表中选择记录！", MessageBox.Icon.WARNING);
            return;
        } 
        if(tfManager_status.SelectedItem.Value == "")
        {
            Err.Warning("请选择审核状态！", MessageBox.Icon.WARNING);
            return;
        }
        if (tfmanager_remark.Text.Trim() == "")
        {
            Err.Warning("批复内容不能为空！", MessageBox.Icon.WARNING);
            return;
        }
        CxCompanyPingJium cx = new CxCompanyPingJium("id", tfid.Value.ToString());
        cx.ManagerRemarkTime = DateTime.Now;
        cx.ManagerRemark = tfmanager_remark.Text;
        cx.ManagerStatus = int.Parse(tfManager_status.SelectedItem.Value);
        cx.Save();
        //日志记录
        LogRecord.Add(new Guid(guid.Value.ToString()), int.Parse(Cookies.getCookies("cuid")), tfmanager_remark.Text,
                      int.Parse(tfManager_status.SelectedItem.Value));
        Clearform();
         
        Err.Warning("批复成功！", MessageBox.Icon.NONE);
    }
    protected void Clearform()
    {
        tfid.Clear();
        tfunfee_time.Text = "";
        tfunfee_detail.Text = "";
        tfunfee_cash.Text = "";
        tfuncoop_peak_time.Text = "";
        tfuncoop_peak_detail.Text = "";
        tfuncoop_others.Text = "";
        tfuncoop_limit_time.Text = "";
        tfuncoop_limit_detail.Text = "";
        tfthief_time2.Text = "";
        tfthief_time1.Text = "";
        tfthief_detail.Text = "";
        tfthief_cash.Text = "";
        //tfdept_master_time.Text = "";
        tfdept_master_remark.Text = "";
        tfcompanyname.Text = "";
        tfcompanyid.Text = "";
        tfmanager_remark.Text = "";
        //tfmanager_remark_time.Text = "";
        tfdeptmaster_status.Reset();
        guid.Clear();
        tfother_master_status.Reset();
        tfother_master_remark.Text = "";
        tfother_master_id.Reset();
        tfManager_status.Reset();
    }
    protected void Store2Databind()
    {
        Query q = new Query(CxUserBase.Schema);
        q.SetSelectList("username,uid");
        q.AddWhere("depid", 1);
        q.AddWhere("levelid", 1);
        Store2.DataSource = q.ExecuteDataSet();
        Store2.DataBind();
    }
    protected void Store3Databind()
    {
        Query q = new Query(CxUserBase.Schema);
        q.SetSelectList("username,uid");
        //q.AddWhere("depid", 1);
        q.AddWhere("levelid", 1);
        Store3.DataSource = q.ExecuteDataSet();
        Store3.DataBind();
    }
}

